package com.dw.etl.core.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dw.etl.core.bean.vo.LayMsg;
import com.dw.etl.core.bean.vo.Msg;
import com.dw.etl.core.bean.model.SysPermissions;
import com.dw.etl.core.api.CoreAuthorizationService;
import com.dw.etl.core.api.PermissionsService;
import com.dw.etl.core.utils.Ognlutis;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author GuoPeng
 * @Title: PermissionsController
 * @Description: 权限分配管理
 * @date 2019/5/13 21:42
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
public class PermissionsController {


    @Resource
    private PermissionsService permissionsService;

    @Resource
    private CoreAuthorizationService coreAuthorizationService;


    //为角色加权限
    @RequestMapping("/RolesSetPermissions")
    @ResponseBody
    public Msg RolesSetPermissions(String LoginKey, String data, Integer id) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            Map map = (Map) gson.fromJson(data, new TypeToken<Map>() {
            }.getRawType());
            return permissionsService.RolesSetPermissions(map, id);
        } else {
            return Msg.fail();
        }
    }


    //获取所有权限
    @RequestMapping("/getPermissionsAll")
    @ResponseBody
    public Msg getPermissionsAll(String LoginKey, Integer id) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            return permissionsService.selectAll(id);
        } else {
            return Msg.fail();
        }
    }


    //以下为权限的增删改查
    @RequestMapping("/insertPermissions")
    @ResponseBody
    public Msg insertPermissions(String LoginKey, String data) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            SysPermissions sysPermissions = (SysPermissions) gson.fromJson(data, new TypeToken<SysPermissions>() {
            }.getRawType());
            try {
                return permissionsService.insertPermissions(sysPermissions);
            } catch (Exception e) {
                return Msg.fail().add("info", "权限名称已存在！");
            }

        } else {
            return Msg.fail();
        }
    }


    @RequestMapping("/getlistPermissions")
    @ResponseBody
    public LayMsg getlistPermissions(String LoginKey, Integer page, Integer limit) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            return permissionsService.getPermissionsList(page, limit);
        } else {
            return LayMsg.fail();
        }
    }

    @ResponseBody
    @RequestMapping("/updatePermissions")
    public Msg updatePermissions(String LoginKey, String permissions) {
        Gson gson = new Gson();
        SysPermissions sysPermissions = (SysPermissions) gson.fromJson(permissions, new TypeToken<SysPermissions>() {
        }.getRawType());
        Msg msg = coreAuthorizationService.autLoginToken(LoginKey);
        if (msg.getCode() == 100) {
            return permissionsService.updatePermissions(sysPermissions);
        } else {
            return msg;
        }
    }


    @RequestMapping("/PermissionsLock")
    @ResponseBody
    public Msg PermissionsLock(String LoginKey, Integer id, Boolean lock) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            SysPermissions sysPermissions = new SysPermissions();
            sysPermissions.setId(id);
            sysPermissions.setAvailable(lock);
            return permissionsService.PermissionsLock(sysPermissions);
        } else {
            return Msg.fail();
        }
    }


    @RequestMapping("/delectPermissions")
    @ResponseBody
    public Msg delectPermissions(String LoginKey, String permissions) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            List<Map> maps = (List) gson.fromJson(permissions, new TypeToken<List<Map>>() {
            }.getRawType());
            for (int i = 0; i < maps.size(); i++) {
                Integer id = Ognlutis.getNumber("id", maps.get(i)).intValue();
                Msg delectuser = permissionsService.delectPermissions(id);
            }
            return Msg.success();
        }
        return Msg.fail();
    }


}
